package com.longrise.android.byjk.plugins.tabfirst;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.glide.RoundCornersTransformation;
import com.longrise.android.byjk.utils.TimeHelper;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.UmengStatisticsUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by callxkj on 2017/11/23.
 * 费试试看的样式
 */

public class HomeRcvItem7Adapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    private List<EntityBean> mListBeans = new ArrayList<>();
    private OnItem7ClickListener mItem2ClickListener;

    public HomeRcvItem7Adapter() {
        super(R.layout.item_homercv7, null);
    }

    public void refreshData(EntityBean[] beans) {
        if (beans == null) {
            return;
        }
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(mListBeans);
    }

    /**
     * 设置图片的点击事件
     */
    public void setOnItem7ClickListener(OnItem7ClickListener listener) {
        this.mItem2ClickListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, EntityBean item) {
        final int position = helper.getLayoutPosition();

        TextView tv_name = helper.getView(R.id.item_homercv7_name_tv);
        TextView tv_time = helper.getView(R.id.item_homercv7_time_tv);
        ImageView iv = helper.getView(R.id.item_homercv7_iv);
        RelativeLayout rl = helper.getView(R.id.item_homercv7_rl);

        EntityBean entityBean = mListBeans.get(position);
        String picpath = entityBean.getString("realpic");
        String name = entityBean.getString("name");
        String time = entityBean.getString("grouptime");
        final String courseid = entityBean.getString("courseid");
        final String realprice = entityBean.getString("realprice");

        RoundCornersTransformation transformation =
                new RoundCornersTransformation(mContext,
                        AppUtil.dip2px(4),
                        RoundCornersTransformation.CornerType.ALL);
        Glide.with(AppUtil.getContext())
                .load(picpath)
                .placeholder(R.drawable.by_home_tuijian)
                .bitmapTransform(transformation)
                .into(iv);
        tv_name.setText(name);
        tv_time.setText(TimeHelper.formateTime(time));
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItem2ClickListener != null) {
                    mItem2ClickListener.onClick(courseid, realprice);
                    UmengStatisticsUtil.onEvent("freecourse_recommend"+(helper.getLayoutPosition()+1));
                }
            }
        });
    }


    interface OnItem7ClickListener {
        void onClick(String courseid, String realprice);
    }
}
